package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.dao.CommandRecordMapper;
import com.wtwd.campus.entity.*;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.DeviceSettingService;
import com.wtwd.campus.utils.ReceiverUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
@MsgType(value = Constants.VOLUME_ADJUSTMENT)
public class VolumeAdjustmentReceiverHandler implements IReceiverHandler {
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private DeviceSettingService deviceSettingService;
    @Resource
    private CommandRecordMapper commandRecordMapper;
    @Autowired
    private DeviceLogService deviceLogService;
    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqGeneralValueResult mqGeneralValueResult = (MqGeneralValueResult) mqMessage.getObject();
        String imei = mqGeneralValueResult.getImei();
        String commandNumber = mqGeneralValueResult.getCommandNumber();
        String speakLevel = mqGeneralValueResult.getValue();
        //处理指令回复的时间
        String date = mqGeneralValueResult.getDeviceDate();
        String time1 = mqGeneralValueResult.getDeviceTime();
        Long time = getTime(date, time1);
        //根据流水号，imei号，和指令类型找到唯一的一条指令记录
        CommandRecord commandRecord = commandRecordMapper.getCommandRecordByCondition2(mqGeneralValueResult.getImei(), mqGeneralValueResult.getCommandNumber(), Constants.VOLUME_ADJUSTMENT);
        if (commandRecord!=null){
            DeviceInfo deviceInfo = deviceInfoService.getDeviceByImei(imei);
            if (deviceInfo!=null){
                deviceInfo.setLastUpTime((new Date()).getTime());//最后上传时间
                deviceInfoService.modifyDeviceInfo(deviceInfo);
                //修改设备设置信息
                DeviceSetting deviceSetting = deviceSettingService.getDeviceSettingByDeviceId(deviceInfo.getDeviceId());
                if(deviceSetting != null){
                    deviceSetting.setSpeakLevel(Integer.parseInt(speakLevel));
                    deviceSettingService.modifyDeviceSetting(deviceSetting);
                }
                //将指令状态改为完成
                commandRecordMapper.completeCommand(commandRecord.getId(), System.currentTimeMillis());
                //如果定位数据不为空,则处理定位数据
                LocationDataVo locationDataVo = null;
                if (mqGeneralValueResult.getMqLocationData() != null) {
                    //处理指令回复的时间
                    if (mqGeneralValueResult.getMqLocationData().getLatitude() != 0 && mqGeneralValueResult.getMqLocationData().getLongitude() != 0) {
                        //进行定位数据存储以及围栏告警记录的存储
                        deviceInfo = receiverUtils.dealLocation(mqGeneralValueResult.getMqLocationData(), time, deviceInfo,1);
                        locationDataVo = new LocationDataVo();
                        locationDataVo.setLocationType(1);
                        locationDataVo.setLatitude(mqGeneralValueResult.getMqLocationData().getLatitude().toString());
                        locationDataVo.setLongitude(mqGeneralValueResult.getMqLocationData().getLongitude().toString());
                        locationDataVo.setAddress(deviceInfo.getAddress());
                        locationDataVo.setImei(imei);
                        locationDataVo.setLocationTime(time);
                    }
                }
                String delKey=Constants.VOLUME_ADJUSTMENT.concat("-").concat(commandNumber);
                receiverUtils.deleteCommandInRedis(imei, delKey);
                //存储设备接收日志
                DeviceLog deviceLog = new DeviceLog();
                deviceLog.setSendUser(Constants.DEVICE_USER);
                deviceLog.setImei(imei);
                deviceLog.setBigType(Constants.MESS_TYPE);
                deviceLog.setType(Constants.VOLUME_ADJUSTMENT);
                JSONObject contant = new JSONObject();
                contant.put("设备号", imei);
                contant.put("音量",speakLevel);
                contant.put("设备时间", mqGeneralValueResult.getDeviceDate());
                contant.put("经度",mqGeneralValueResult.getMqLocationData().getLongitude());
                contant.put("纬度",mqGeneralValueResult.getMqLocationData().getLatitude());
                contant.put("速度",mqGeneralValueResult.getMqLocationData().getSpeed());
                contant.put("方位",mqGeneralValueResult.getMqLocationData().getOrientation());
                contant.put("设备状态",mqGeneralValueResult.getDeviceStatus());
                deviceLog.setCmd(contant.toJSONString());
                deviceLog.setSendTime(System.currentTimeMillis());
                deviceLog.setCommandNumber(commandNumber);
                deviceLog.setState(1);
                deviceLogService.addDeviceLog(deviceLog);
            } else {
                saveUnInStorage(imei);
                receiverUtils.cloaseChannel(imei);
            }
        }
    }
}
